{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir(\"E:\\\\pythonstudy\\\\abcd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#实现单层目录大小统计\n",
    "def file_size(dir):\n",
    "    dist=os.listdir(dir)#取得目录下的所有文件列表\n",
    "    fize=0#初始化文件大小\n",
    "    for f in dist:\n",
    "        if os.path.isfile(f):#判断是否属于文件\n",
    "            si=os.path.getsize(f)#取得文件大小\n",
    "            fize+=si\n",
    "    return fize\n",
    "h=file_size(\"E:\\\\pythonstudy\\\\abcd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "469063500\n"
     ]
    }
   ],
   "source": [
    "print(h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "938127000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#实现多层目录大小统计\n",
    "def file_size2(dir):\n",
    "    dist=os.listdir(dir)\n",
    "    fize=0\n",
    "    for f in dist:\n",
    "\n",
    "        if os.path.isdir(f):#判断是否属于目录，如果是目录则进行递归操作\n",
    "            fize+=file_size2(f)#递归\n",
    "        \n",
    "        elif os.path.isfile(f):#判断是否属于文件\n",
    "            si=os.path.getsize(f)\n",
    "            fize+=si\n",
    "        \n",
    "    return fize\n",
    "file_size2(\"E:\\\\pythonstudy\\\\abcd\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
